package io.renren.modules.job.task;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import com.itextpdf.text.DocumentException;

import cn.hutool.core.text.StrFormatter;
import io.renren.modules.app.entity.ShepiQm;
import io.renren.modules.app.entity.ibps.BpmExec;
import io.renren.modules.app.entity.ibps.BpmInstHis;
import io.renren.modules.app.entity.nosql.LcPdfTemplate;
import io.renren.modules.app.nosql.mongo.ILcPdfTemplate;
import io.renren.modules.app.nosql.mongo.IPdfRepo;
import io.renren.modules.app.service.IBpmExecService;
import io.renren.modules.app.service.IBpmInstHisService;
import io.renren.modules.app.service.IBxMxService;
import io.renren.modules.app.service.IBxService;
import io.renren.modules.app.service.IEmployeeService;
import io.renren.modules.app.service.IQjService;
import io.renren.modules.app.service.huizhou.IHuiyiDengji;
import io.renren.modules.app.service.huizhou.IWaichuBaozhang;
import io.renren.modules.app.service.huizhou.IWaichuShenqing;
import io.renren.modules.job.task.pdf.IPdf;
import io.renren.modules.job.task.pdf.PdfBx;
import io.renren.modules.job.task.pdf.PdfQj;
import io.renren.modules.job.task.pdf.huizhou.Baozhang;
import io.renren.modules.job.task.pdf.huizhou.Waichu;
import jodd.io.FileUtil;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;

/**
 * pdf生成
 * 
 * @author Administrator
 */
@Component("taskPdf")
@Slf4j
public class TaskPdf {

	@Autowired
	IBpmInstHisService lcHisService;

	@Autowired
	IQjService qjService;

	@Autowired
	IBpmExecService bpmExecService;

	@Autowired
	IEmployeeService userService;

	@Autowired
	IBxService bxService;
	@Autowired
	IBxMxService bxMxService;

	@Autowired
	IPdfRepo pdfRepo;

	// 惠州第一中学外出申请
	@Autowired
	IWaichuShenqing waichuRepo;
	@Autowired
	IWaichuBaozhang baozhangRepo;
	@Autowired
	IHuiyiDengji huiyiDengjiRepo;

	@Autowired
	ILcPdfTemplate templateService;

	@Value("${pafBasePath}")
	String pdfBasePath;

	public static final String fileServer = "129.204.27.177:8567";
	public static final String picServer = "http://129.204.27.177:4869/";

	@Synchronized
	public void genPdf(String params) {
		Arrays.asList(params.split(",")).forEach(process -> {
			try {
				File pdfFolder = new File(StrFormatter.format("{}/{}", pdfBasePath, process));
				if (!FileUtil.isExistingFile(pdfFolder)) {
					FileUtil.mkdir(pdfFolder);
				}
				this.gen(process);
			} catch (Exception e) {
				log.error("生成pdf异常", e);
			}
		});
	}

	private void gen(String process) {
		// 1.获取所有完成的工作流
		List<BpmInstHis> his = lcHisService.findIBPS_BPM_INST_HIS(process);
		his.forEach(hi -> {
			// 2.通过工作流id获取所有的节点
			String idLiucheng = hi.getId();
			List<BpmExec> nodes = bpmExecService.findBpmByINSTId(idLiucheng);
			
			// 3.通过节点找出谁授权通过的,用于区分级长跟校长是谁
			Map<String, Object> paramMap = new HashMap<>();
			paramMap.put("picServer", picServer);
			for (BpmExec node : nodes) {
				String name = node.getNodeName();
				log.info("节点名称:" + name);
				// 4.找出账户
				userService.findById(node.getExerIid()).ifPresent(user->{
					paramMap.put(name, new ShepiQm(name, user));
					log.info("参数,key:{},val:{}",name,user);
				});
			}
			// 文件上传
			File pdf = new File(StrFormatter.format("{}/{}/{}.pdf", pdfBasePath, process, hi.getSubject().replace(":", "")));

			LcPdfTemplate lcSearch = new LcPdfTemplate().setKey(process);
			templateService.findOne(Example.of(lcSearch)).ifPresent(lcTemplate->{
				IPdf iPdf = null;
				if ("Process_27d5k7a".equals(process)) {
					iPdf = new PdfQj(idLiucheng, pdf, process, paramMap, pdfRepo, qjService, lcTemplate);
				} else if ("Process_baoxiao".equals(process)) {
					iPdf = new PdfBx(hi.getBiz_key(), pdf, process, paramMap, pdfRepo, bxService, bxMxService, lcTemplate);
				} else if ("Process_waichu".equals(process)) {
					iPdf = new Waichu(idLiucheng, pdf, process, paramMap, pdfRepo, waichuRepo, lcTemplate);
				} else if ("P_baozhang".equals(process)) {
					iPdf = new Baozhang(idLiucheng, pdf, process, paramMap, pdfRepo, baozhangRepo, lcTemplate);
				} else if ("P_huiyidengji".equals(process)) {
					iPdf = new io.renren.modules.job.task.pdf.huizhou.HuiyiDengji(idLiucheng, pdf, process, paramMap, pdfRepo, huiyiDengjiRepo,
							lcTemplate);
				}
				try {
					iPdf.gen();
				} catch (IOException | DocumentException | com.lowagie.text.DocumentException e) {
					e.printStackTrace();
				}	
			});
		});
	}
}
